package com.hos.check.util;

import org.apache.commons.lang.StringUtils;

/**
 * 参考.net 加密,达到密码一致
 * @author zhagntian
 *
 */
public class EncryptionUtil {

	/**
	 * 加密算法
	 * 
	 * @param password
	 * @return
	 */
	public static String encryptString(String Text) {
		if (StringUtils.isEmpty(Text))
			return "";
		int il_bit, il_x, il_y, il_len, i;
		String is_out;
		il_len = Text.length();
		il_x = 0;
		il_y = 0;
		is_out = "";
		for (i = 0; i < il_len; i++) {

			il_bit = (int) Text.charAt(i);
			il_y = (il_bit * 13) % 256 + il_x;
			is_out = is_out + (char) il_y;
			il_x = il_bit * 13 / 256;
		}
		is_out = is_out + (char) il_x;
		Text = is_out;
		il_len = Text.length();
		il_x = 0;
		il_y = 0;
		is_out = "";
		for (i = 0; i < il_len; i++) {
			il_bit = (int) Text.charAt(i);
			il_y = il_bit / 16 + 64;
			is_out = is_out + (char) il_y;
			il_y = (il_bit % 16) + 64;
			is_out = is_out + (char) il_y;
		}
		return is_out;
	}

	/**
	 * 解密
	 * @param original
	 * @return
	 */
	public static String unEncryptString(String original) {

		 int lenth = original.length();
         if (lenth == 0) return "";
         int x = 0, y = 0;
         String decryptString = "";
         for (int i = 0; i < lenth; i += 2)
         {
             x = (int)original.charAt(i);
             y = (x - 64) * 16;
             y = y + original.charAt(i+1) - 64;
             decryptString = decryptString + (char)y;
         }
         x = 0;
         y = 0;
         original = decryptString;
         decryptString = "";
         lenth = original.length();
         x = (int)original.charAt(original.length()-1);
         for (int i = lenth - 2; i >= 0; i -= 1)
         {
             y = x * 256 + (int)original.charAt(i);
             x = y % 13;
             decryptString = (char)(y / 13) + decryptString;
         }
         return decryptString;
	}

	public static void main(String[] args) {
		
		System.out.println(EncryptionUtil.encryptString("qwe123"));
		System.out.println(EncryptionUtil.unEncryptString("GMHLHLII@B"));
	}

}
